package com.yycwxt.modules.admin.express;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.yycwxt.common.BaseService;
import com.yycwxt.domain.Kdfdzd;
import com.yycwxt.modules.admin.data_import.AccountData;
import com.yycwxt.modules.admin.data_import.ImpFile;
import com.yycwxt.modules.admin.data_import.ImpFileMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ExpressService extends BaseService<ImpFileMapper, ImpFile> {

    private final ExpressMapper expressMapper;

    public ExpressService(@Autowired ExpressMapper expressMapper){
        this.expressMapper = expressMapper;
    }

//    public List<Express> select(String zq){
//        return expressMapper.select(zq);
//    }

    //调用存储过程生成数据
//    @Transactional
//    public void save(ImpFile zq){
//        expressMapper.generateKDF(zq);
//    }

    // 查询账期
    public Object[] selectZq(){
        return expressMapper.selectZq();
    }

    //下载源文件查询url
    public List<ImpFile> selectUrl(){
        return expressMapper.selectUrl();
    }

    //excel导入
//    @SneakyThrows
//    @Transactional
//    public boolean importFileToDB(boolean deleteHistory, MultipartFile file) {
//        if (deleteHistory) {
//            this.remove(null);
//        }
//        EasyExcel.read(file.getInputStream(), Express.class, new ReadListener<Express>() {
//
//            private static final int BATCH_SIZE = 1000;
//            private List<Express> ls = Lists.newArrayList();
//
//            @Override
//            public void invoke(Express data, AnalysisContext context) {
//                // System.out.println(data);
//                ls.add(data);
//                if (ls.size() >= BATCH_SIZE) {
//                    ExpressService.this.saveBatch(ls);
//                    ls.clear();
//                }
//            }
//
//            @Override
//            public void doAfterAllAnalysed(AnalysisContext context) {
//                ExpressService.this.saveBatch(ls);
//            }
//        }).sheet().doRead();
//        return true;
//    }

    //查询快递费表格数据
    public List<Map<String, Object>> selectKdf(String zq) {
        Map<String, Object> map = new HashMap<>();
        map.put("zq", zq);
        map.put("storeNames", new String [] {"金社长仓库", "沈北wms", "湖南仓", "天下云仓", "金社长二仓", "金社长返货仓", "代发仓", "吉客云仓", "华怡3a45b9c6", "赵腾封条供应链187b401c"});
        map.put("storeNames2", new String [] {"顺丰", "中通", "申通"});
        return expressMapper.select(map);
    }


    //上传快递费对账单
    @SneakyThrows
    @Transactional
    public void importKdfdzd(MultipartFile file) {
        EasyExcel.read(file.getInputStream(), Kdfdzd.class, new ReadListener<Kdfdzd>() {

            private static final int BATCH_SIZE = 1000;


            @Override
            public void invoke(Kdfdzd data, AnalysisContext context) {

                // 执行存储过程
                System.out.println(123);
                expressMapper.importKdfdzd(AccountData.getAccountDate(), data.getKdgs(), data.getYdh(),
                        data.getJszl(), data.getQz(), data.getSfcs(),
                        data.getMdsf(), data.getMdcs(), data.getKdf(),
                        data.getDp(),data.getFhsj());
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {

            }
        }).sheet().doRead();
    }
}
